home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / usr (gcc 1.37 libs) / mac / stat.c < prev    next >
C/C++ Source or Header  |  1993-12-08  |  2KB  |  76 lines

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include "crtlocal.h"
  5.  
  6. static int macstat(StringPtr path, struct stat *buf, short nVRefNum, long lDirID )
  7.         {
  8.         CInfoPBRec  cPB;
  9.            bzero(buf, sizeof(struct stat));
  10.         cPB.hFileInfo.ioNamePtr = path;
  11.         cPB.hFileInfo.ioVRefNum = nVRefNum;
  12.         cPB.hFileInfo.ioDirID = lDirID;
  13.         cPB.hFileInfo.ioFDirIndex = 0;
  14.  
  15.         if (PBGetCatInfoSync(&cPB))
  16.             {
  17.             return -1;
  18.             }
  19.               
  20.         /* Type of file: directory or regular file + access */
  21.         
  22.         if (cPB.hFileInfo.ioFlAttrib & ioDirMask)
  23.             {
  24.              buf->st_ino = cPB.dirInfo.ioDrDirID;
  25.             buf->st_mode = S_IFDIR;
  26.               }
  27.         else
  28.             {
  29.             int siz = cPB.hFileInfo.ioFlClpSiz;
  30.             if (!siz) siz = 8192;
  31.             buf->st_blksize = siz;
  32.             buf->st_blocks = (cPB.hFileInfo.ioFlPyLen+siz-1) / siz;
  33.             buf->st_uid = cPB.hFileInfo.ioFlFndrInfo.fdCreator;
  34.             buf->st_gid = cPB.hFileInfo.ioFlFndrInfo.fdType;
  35.              buf->st_ino = cPB.hFileInfo.ioFRefNum;
  36.             buf->st_size = cPB.hFileInfo.ioFlLgLen;
  37.              buf->st_flags = cPB.hFileInfo.ioFlFndrInfo.fdFlags;
  38.             buf->st_mode = S_IFREG;
  39.                }
  40.         buf->st_mode |= cPB.hFileInfo.ioFlAttrib & 0x01 ? S_IREAD : (S_IREAD | S_IWRITE);
  41.         /* last access time, modification time and creation time(?) */
  42.         buf->st_atime = buf->st_mtime = unixTime(cPB.hFileInfo.ioFlMdDat);
  43.         buf->st_ctime = unixTime(cPB.hFileInfo.ioFlCrDat);
  44.         buf->st_dev = (long)cPB.hFileInfo.ioVRefNum;
  45.         buf->st_nlink = 1;
  46.         return 0;
  47.         }
  48.     
  49. int fstat(int fd, struct stat *buf)
  50.     {
  51.     FCBPBRec pb;
  52.     Str255 name;
  53.     int refnum = crt_fd_tab[fd].fd;
  54.     if (crt_fd_tab[fd].flags & O_PIPE)
  55.         {
  56.            bzero(buf, sizeof(struct stat));
  57.         buf->st_mode = S_IFIFO;
  58.         buf->st_blksize = 8192;
  59.         return 0;
  60.         }
  61.     mysleep(1);
  62.     pb.ioRefNum = refnum;
  63.     pb.ioFCBIndx = 0;
  64.     pb.ioCompletion = 0;
  65.     pb.ioVRefNum = crt_ioVRefNum;
  66.     pb.ioNamePtr = (StringPtr) name;
  67.     PBGetFCBInfoSync(&pb);
  68.     return macstat(name, buf, pb.ioFCBVRefNum, pb.ioFCBParID);
  69.     }
  70.  
  71. int stat(const char *name, struct stat *buf)
  72.     {
  73.     StringPtr s = cnv_unix_name(name);
  74.     return macstat(s, buf, crt_ioVRefNum, 0);
  75.     }
  76.